package hxm.monads;

/**
 * This is not really working yet (and possibly pointless in a non-pure environment).
 * 
 * @author Simon Krajewski
 */

typedef State<S, A> = S -> { a:A, s:S };

class StateMonad 
{
	static public function bind <S, A, B>(a:State<S, A> , f:A->State<S, B>):State<S, B>
	{
		return function(s)
		{
			var oldState = a(s);
			return f(oldState.a)(oldState.s);
		};
	}
	
	static public function yield<S, A>(a:A):State<S, A>
	{
		return function(s) return { a:a, s:s };
	}
	
	static public function get<S>():State<S, S> return function(s:S) return { a:s, s:s }
	static public function put<S, A>(s:S):State<S, A> return function(?n) return { a:null, s:s }
}